﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Biz_Dal.Biz;
using System.Drawing;
using System.Drawing.Imaging;

public partial class Admin_Admin_QL_SACH : System.Web.UI.Page
{
    BizSach sach;
    protected void Page_Init(object sender, EventArgs e)
    {
        if (Session["Dang_nhap"] != "1")
        {
            Response.Redirect("~/Trang_web/Trang_chu.aspx");
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        string jsScript = "<script language='JavaScript' src='confirm.js'></script>";
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "clientScript", jsScript);
    }
    protected void gridview_Sach_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        //gridview_Sach.PageIndex = e.NewPageIndex;
        //string caulenh = "select * from sach order by " + ViewState["Cotxs"];
        //gridview_Sach.DataSource = new XL_SACH(caulenh);
        //gridview_Sach.DataBind();
    }
    protected void gridview_Sach_Sorting(object sender, GridViewSortEventArgs e)
    {
        //if (ViewState[e.SortExpression.ToString()].ToString() == "asc")
        //    ViewState[e.SortExpression.ToString()] = "desc";
        //else
        //    ViewState[e.SortExpression.ToString()] = "asc";
        //string caulenh = "select * from sach order by " +
        //   e.SortExpression + " " + ViewState[e.SortExpression.ToString()].ToString();
        //gridview_Sach.DataSource = new XL_SACH(caulenh);
        //gridview_Sach.DataBind();
        //ViewState["Cotxs"] = e.SortExpression + " " + ViewState[e.SortExpression.ToString()].ToString();
    }
    protected void btnCapNhat_Click(object sender, EventArgs e)//update
    {
        sach = BizSach.getSachById(Convert.ToInt32(LabelId.Text));
        sach.maSach = txtMaSach.Text;
        sach.tenSach = txtTenSach.Text;
        sach.tacGia = txtTacGia.Text;
        sach.ndSach = txtNoiDung.Value;
        sach.giaSach = Convert.ToDouble(txtGiaSach.Text);
        sach.idTheLoai = Convert.ToInt32(DropDownListTheLoai.SelectedValue.ToString());
        sach.idNXB = Convert.ToInt32(DropDownListNXB.SelectedValue.ToString());
        sach.giaSach = Convert.ToDouble(txtGiaSach.Text);
        sach.updateSach();
        gridview_Sach.DataBind();
        if (FileUpload1.HasFile && FileUpload1.PostedFile.ContentLength > 0)
        {
            //Read the file in to a byte Array.
            byte[] filebyte = FileUpload1.FileBytes;
            BizSach.updateImage(Convert.ToInt32(LabelId.Text.ToString()), filebyte);
            Load_anh();
        }
    }
    protected void gridview_Sach_SelectedIndexChanged(object sender, EventArgs e)
    {
        NewDropDownList();
        update();
        PanelThongTin.Visible = true;
        GridViewRow row = gridview_Sach.SelectedRow;
        int id = Convert.ToInt32(row.Cells[0].Text.ToString());
        sach = BizSach.getSachById(id);
        LabelId.Text = sach.idSach.ToString();
        txtMaSach.Text = sach.maSach;
        txtTenSach.Text = sach.tenSach;
        txtTacGia.Text = sach.tacGia;
        txtNoiDung.Value = sach.ndSach;
        txtGiaSach.Text = sach.giaSach.ToString();
        Load_anh();
        DropDownListTheLoai.SelectedValue = sach.idTheLoai.ToString();
        DropDownListNXB.SelectedValue = sach.idNXB.ToString();
    }
    protected void btnThem_Click(object sender, EventArgs e)
    {
        New();
    }
    private void New()
    {
        insert();
        sach = null;
        PanelThongTin.Visible = true;
        int id = BizSach.IdMaxSach();
        id++;
        LabelId.Text = id.ToString();
        txtMaSach.Text = null;
        txtTenSach.Text = null;
        txtTacGia.Text = null;
        txtNoiDung.Value = null;
        txtGiaSach.Text = null;
        NewDropDownList();
    }
    protected void btnLMoi_Click(object sender, EventArgs e)//insert
    {
        sach = new BizSach();
        sach.maSach = txtMaSach.Text;
        sach.tenSach = txtTenSach.Text;
        sach.tacGia = txtTacGia.Text;
        sach.ndSach = txtNoiDung.Value;
        sach.idTheLoai=Convert.ToInt32(DropDownListTheLoai.SelectedValue.ToString());
        sach.idNXB = Convert.ToInt32(DropDownListNXB.SelectedValue.ToString());
        sach.giaSach = Convert.ToDouble(txtGiaSach.Text);
        BizSach.insertSach(sach);
        int id = BizSach.IdMaxSach();
        LabelId.Text = id.ToString();
        if (FileUpload1.HasFile && FileUpload1.PostedFile.ContentLength > 0)
        {
            //Read the file in to a byte Array.
            byte[] filebyte = FileUpload1.FileBytes;
            BizSach.updateImage(id, filebyte);
            Load_anh();
        }
        update();
        gridview_Sach.DataBind();
    }

    private void NewDropDownList()
    {
        List<BizTheLoai> theLoai = new List<BizTheLoai>();
        theLoai = BizTheLoai.getListTheLoaiAll();
        DropDownListTheLoai.DataSource = theLoai;
        DropDownListTheLoai.DataTextField = "tenTheLoai";
        DropDownListTheLoai.DataValueField = "idTheLoai";
        DropDownListTheLoai.DataBind();
        DropDownListTheLoai.SelectedIndex = 0;
        List<BizNXB> nxb = new List<BizNXB>();
        nxb = BizNXB.getListNXBAll();
        DropDownListNXB.DataSource = nxb;
        DropDownListNXB.DataTextField = "tenNXB";
        DropDownListNXB.DataValueField = "idNXB";
        DropDownListNXB.DataBind();
        DropDownListNXB.SelectedIndex = 0;
    }

    private void Load_anh()
    {
        if(sach.hinhSach!=null)
            Image_sach.ImageUrl = "~/MyPhoto.ashx?ID=" + LabelId.Text;
        else
            Image_sach.ImageUrl = "~/Hinh/images.jpg";
    }

    
    protected void btnXoa_Click(object sender, EventArgs e)
    {
        BizSach.deleteSach(txtMaSach.Text);
        New();
        Image_sach.ImageUrl = "~/Hinh/images.jpg";
        gridview_Sach.DataBind();
    }

    #region "btn"
    private void insert()
    {
        btnCapNhat.Visible = false;
        btnLMoi.Visible = true;
        btnXoa.Visible = false;
    }
    private void update()
    {
        btnXoa.Visible = true;
        btnCapNhat.Visible = true;
        btnLMoi.Visible = false;
    }
    #endregion
}